package com.example.onion.hxchat.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * 聊天记录保存本地的sqlite帮助类
 * 加载？: DBhelper = new MyDatabaseHelper(this,"ChatLog.db",null,1);
 * ChatLog.db是数据库名  可以建立很多表
 * 插入数据： SQLiteDatabase db = DBhelper.getWritableDatabase();
 * ContentValues values = new ContentValues();
 * values.put("表字段名","value");
 * db.insert("表名",null,values);  保存聊天记录的表名：chatDb
 * values.clear(); 清楚数据可以继续插入第二、三....条数据
 * 查询： SQLiteDatabase db = DBhelper.getWritableDatabase();
 * Cursor cursor = db.query("表名",null,null,null,null,null,null);
 * //7个参数  只指定表名表示查询该表的所有数据
 * if(cursor.moveToFirst()){
 * do{
 * //遍历Cursor对象，取出数据
 * String xx = cursor.getString(cursor.getColumnIndex("表的字段名"));
 * int xx = cursor.getInt(cursor.getColumnIndex("表的字段名"));
 * }while(cursor.moveToNext());
 * }
 * cursor.close();
 * <p>
 * <p>
 * 居然可以直接用sql操作，害我打这么多
 * 前面是sql，后面给定问号的值，不过应该也可以不用问号  直接给定问号的值吧,适用于确定的sql操作
 * db 应该是 DBhelper = new MyDatabaseHelper(this,"local.db",null,1);
 * |------> SQLiteDatabase db = DBhelper.getWritableDatabase();
 * 添加：db.execSQL("insert into xxx(字段1,字段1,字段1) values(?,?,?)" , new String[]{"value1","value2","value3"});
 * 更新：db.execSQL("update xxx set 字段1 = ？ where 字段2 = ？" , new String[]{"问号的值","问号的值"})
 * 删除：db.execSQL("delete from xxx where 字段1 （> = <） ？",new String[]{"  "})
 * 查询：db.rawQuery(select * from xxx , null)   没问号，后面指定参数指定为null
 *
 * @author:onion
 * @data: 2020/6/20
 */
public class MyLocalDatabaseHelper extends SQLiteOpenHelper {
    /**
     * 创建聊天记录的数据库sql语句
     * text 文本类型   integer 整形   real 浮点   blob 二进制 autoincrement自增长
     */
    public static final String CREATE_CHATDB = "create table chatDb (" +
            "id integer primary key autoincrement," +
            "userId text," +
            "content text," +
            "toUserId text," +
            "type integer," +
            "time text," +
            "userName text)";
    private String name;//当前会话界面的用户昵称

    private String info;//当前会话界面的最后一条消息（实现不了就算了)

    private String userId;//当前会话界面的用户id

    public static final String CREATE_INFOITEM = "create table infoItem (" +
            "id integer primary key autoincrement," +
            "userName text," +
            "lastInfo text," +
            "userId text," +
            "ownerUserId text)";
    public static final String CREATE_NOTIFICATION = "create table notification (" +
            "id integer primary key autoincrement," +
            "userName text," +
            "userId text," +
            "applyContent text)";

    public static final String CREATE_FRIEND = "create table friend (" +
            "id integer primary key autoincrement," +
            "userName text," +
            "userId text)";


    public static final String CREATE_USER = "create table user ("+
            "userID text primary key,"+
            "userPwd text," +
            "userName text," +
            "userAge integer," +
            "userGrade text," +
            "userSpy text," +
            "userEmail text," +
            "userPid text," +
            "userTid text," +
            "userMoney double," +
            "userLabel1 text," +
            "userLabel2 text," +
            "userLabel3 text," +
            "userLabel4 text)";


    private Context mContext;

    public MyLocalDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    /**
     * DBhelper = new MyDatabaseHelper(this,"local.db",null,1);调用（建表）
     * 如果想再往数据库中加入新的表，由于local.db已经存在，onCreate方法不会再次执行，
     * 因此新添加的表无法得到创建，则需要加大数据库的版本号，然后会执行upgrade方法
     *
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_CHATDB);
        db.execSQL(CREATE_INFOITEM);
        db.execSQL(CREATE_NOTIFICATION);
        db.execSQL(CREATE_FRIEND);
        db.execSQL(CREATE_USER);
        Log.d("ChatHistoryDb", "创建db成功");
    }

    /**
     * DBhelper = new MyDatabaseHelper(this,"local.db",null,1);
     * 第四个参数是数据库版本号，如果需要添加新的表，只需传入比之前传入的版本号大就可以让onUpgrade方法执行
     * 最好一个数据库就一个表吧，一开始就确定好表的基本结构，不要更改，这个方法是删除原先的表，数据会丢失
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists chatDb");
        db.execSQL("drop table if exists infoItem");
        db.execSQL("drop table if exists notification");
        db.execSQL("drop table if exists friend");
        db.execSQL("drop table if exists user");
        onCreate(db);
    }
}

